package com.cs.study.homework._0307;

public class Demo3 {
    public static void main(String[] args) {
        Sell sell = new Sell();
        Thread t1 = new Thread(sell);
        Thread t2 = new Thread(sell);
        Thread t3 = new Thread(sell);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();

    }
}

class Sell implements Runnable {
    static int ticket = 0;
    //Object o = new Object();
    int i = 0;

    @Override
    public void run() {
        while (true) {
            if (i % 2 == 0) {
                synchronized (Sell.class) {
                    if (ticket < 100) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        System.out.println(Thread.currentThread().getName() + "售出第" + ++ticket + "张票");
                    }
                }
            }else {
                load();
            }
            i++;
        }
    }
    public static synchronized void load() {
        if (ticket < 100) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "售出第" + ++ticket + "张票");
        }
    }
}
